package cn.dt.ymcc.config;

import cn.dt.ymcc.domain.Permission;
import cn.dt.ymcc.service.ILoginService;
import cn.dt.ymcc.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.List;

/**
 * Security配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启方法授权
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private IPermissionService permissionService;

    public static void main(String[] args) {
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
//        String password1 = bc.encode("123");
//        String password2 = bc.encode("123");
//        System.out.println(password1);
//        System.out.println(password2);
        boolean isOk = bc.matches("123", "$2a$10$N34XIq9t6KsrkpqHS3gUvO9tbwX9ZMGuFf0/PXv5g.uTFxg.vC5XW");
        boolean isOk1 = bc.matches("123", "$2a$10$fjuX6vbb2HmcEoKdsGSOiOIXNyg4sfLHId704nGfnRDLlwBiwV/Zy");
        System.out.println(isOk);
        System.out.println(isOk1);
    }

    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
    //密码编码器：不加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    
    //授权规则配置

    /**
     * 需要配置哪些资源需要什么权限才能访问
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //web授权 --即：将t_permission表中的数据，交给SpringSecurity
        List<Permission> permissions = permissionService.selectList(null);
        permissions.forEach(permission -> {
            try {
                http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getSn());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        http.authorizeRequests()                                //授权配置
                .antMatchers("/login/common").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }
}